package com.smarttest.portal.service.impl;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smarttest.common.util.HbaseUtils;
import com.smarttest.portal.mapper.TbHbaseMapper;
import com.smarttest.portal.mapper.TbHtableMapper;
import com.smarttest.portal.mapper.TbRowkeyruleMapper;
import com.smarttest.portal.pojo.TbHbase;
import com.smarttest.portal.pojo.TbHbaseExample;
import com.smarttest.portal.pojo.TbHbaseExample.Criteria;
import com.smarttest.portal.pojo.TbHtable;
import com.smarttest.portal.pojo.TbHtableExample;
import com.smarttest.portal.pojo.TbRowkeyrule;
import com.smarttest.portal.pojo.TbRowkeyruleExample;
import com.smarttest.portal.service.DataProviderService;
@Service
public class DataProviderServiceImpl implements DataProviderService {
	@Autowired
	private TbHbaseMapper tbHbaseMapper;
	@Autowired
	private TbHtableMapper tbHtableMapper;
	@Autowired
	private TbRowkeyruleMapper tbRowkeyruleMapper;
	@Override
	public String getHbases() {
		// TODO Auto-generated method stub
		String hbases = "";
		TbHbaseExample example = new TbHbaseExample();
		Criteria critera = example.createCriteria();
		critera.getAllCriteria();
		List<TbHbase> l = tbHbaseMapper.selectByExample(example);
		for (int i = 0;i < l.size(); i++) {
			String name = l.get(i).getName();
			Integer id = l.get(i).getId();
			if (i == 0) {
				hbases += "<option value=\""+id+"\" selected=\"selected\">"+name+"</option>";
			}
			else {
				hbases += "<option value=\""+id+"\">"+name+"</option>";
			}
		}
		return hbases;
	}
	@Override
	public String[] getHbaseNames() {
		// TODO Auto-generated method stub
		TbHbaseExample example = new TbHbaseExample();
		Criteria critera = example.createCriteria();
		critera.getAllCriteria();
		List<TbHbase> l = tbHbaseMapper.selectByExample(example);
		String[] hbasenames = new String[l.size()];
		for (int i = 0;i < l.size(); i++) {
			String name = l.get(i).getName();
			hbasenames[i] = name;
		}
		return hbasenames;
	}
	@Override
	public String[] getHtableNames(int hid) {
		// TODO Auto-generated method stub
		TbHtableExample example = new TbHtableExample();
		com.smarttest.portal.pojo.TbHtableExample.Criteria critera = example.createCriteria();
		critera.andHidEqualTo(hid);
		List<TbHtable> l = tbHtableMapper.selectByExample(example);
		String[] hbasenames = new String[l.size()];
		for (int i = 0;i < l.size(); i++) {
			String name = l.get(i).getTbname();
			hbasenames[i] = name;
		}
		return hbasenames;
	}
	@Override
	public String getHbaseTables(int hbaseid) {
		// TODO Auto-generated method stub
		String htables = "";
		TbHtableExample example = new TbHtableExample();
		com.smarttest.portal.pojo.TbHtableExample.Criteria critera = example.createCriteria();
		critera.andHidEqualTo(hbaseid);
		List<TbHtable> l = tbHtableMapper.selectByExample(example);
		for (int i = 0;i < l.size(); i++) {
			String name = l.get(i).getTbname();
			Integer id = l.get(i).getId();
			if (i == 0) {
				htables += "<option value=\""+id+"\" selected=\"selected\">"+name+"</option>";
			}
			else {
				htables += "<option value=\""+id+"\">"+name+"</option>";
			}
		}
		return htables;
	}
	@Override
	public String getHbasekeys(int tbid) {
		// TODO Auto-generated method stub
		TbRowkeyruleExample example = new TbRowkeyruleExample();
		com.smarttest.portal.pojo.TbRowkeyruleExample.Criteria critera = example.createCriteria();
		critera.andTidEqualTo(tbid);
		List<TbRowkeyrule> l = tbRowkeyruleMapper.selectByExampleWithBLOBs(example);
		String hbasekeys = "";
		for (int i = 0;i < l.size(); i++) {
			String rowkeyrule = l.get(i).getRowkeyrule();
			if (!rowkeyrule.isEmpty()) {
				JSONArray ja = JSON.parseArray(rowkeyrule);
				for (int j = 0; j < ja.size(); j++) {
					JSONObject jo = ja.getJSONObject(j);
					String name = jo.getString("name");
					String type = jo.getString("type");
					String value = "";
					if (jo.containsKey("value")) {
						value = jo.getString("value");
					}
					String typeoptions = "";
					String extra = "";
					if (type.equalsIgnoreCase("string")) {
						typeoptions = "<option value=\"string\" selected=\"selected\">string</option>"
								+ "<option value=\"time\">time</option>"
								+ "<option value=\"timestamp\">timestamp</option>";
					}
					else if (type.equalsIgnoreCase("time")) {
						typeoptions = "<option value=\"string\">string</option>"
								+ "<option value=\"time\" selected=\"selected\">time</option>"
								+ "<option value=\"timestamp\">timestamp</option>";
						String format = jo.getString("format");
						extra = "<section class=\"pull-left no-padding col pull-width120\" style=\"margin: 2px\">"+
								"<label class=\"\">时间格式</label>"+
								"<label class=\"input\"><input i type=\"text\"  class=\"input-sm\" value=\""+format+"\"></label>"+
								"</section>";
					}
					else if (type.equalsIgnoreCase("timestamp")) {
						typeoptions = "<option value=\"string\">string</option>"
								+ "<option value=\"time\">time</option>"
								+ "<option value=\"timestamp\"  selected=\"selected\">timestamp</option>";
						String stamplen = jo.getString("stamplen");
						if (stamplen.equalsIgnoreCase("13")) {
							extra = "<section class=\"pull-left no-padding col pull-width120\" style=\"margin: 2px\">"+
									"<label class=\"\">时间戳长度</label>"+
									"<label class=\"input\"><select class=\"input-sm\"><option selected=\"selected\" value=\"13\">13</option><option value=\"10\">10</option></select></label>"+
									"</section>";
						}
						else {
							extra = "<section class=\"pull-left no-padding col pull-width120\" style=\"margin: 2px\">"+
									"<label class=\"\">时间戳长度</label>"+
									"<label class=\"input\"><select class=\"input-sm\"><option value=\"13\">13</option><option selected=\"selected\" value=\"10\">10</option></select></label>"+
									"</section>";
						}
						
					}
					String reverse = "";
					String autocoverstr = "";
					if (jo.containsKey("reverse") && jo.getString("reverse").equalsIgnoreCase("true")) {
						reverse = "checked=\"checked\"";
					}
					String autocover = "";
					if (jo.containsKey("autocover") && jo.getString("autocover").equalsIgnoreCase("true")) {
						autocover = "checked=\"checked\"";
						String autocoverLen = jo.getString("autocoverLen");
						String autocoverLoc = jo.getString("autocoverLoc");
						String locstr = "";
						if (autocoverLoc.equalsIgnoreCase("before")) {
							locstr = "<option value=\"before\" selected=\"selected\">前</option><option value=\"after\">后</option>";
						}
						else if (autocoverLoc.equalsIgnoreCase("after")) {
							locstr = "<option value=\"before\">前</option><option value=\"after\" selected=\"selected\">后</option>";
						}
						String autocoverMark = jo.getString("autocoverMark");
						autocoverstr = "<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
								"<label class=\"\">位数</label>"+
								"<label class=\"input\">" +
								"<input type=\"text\" maxlength=\"2\" value=\""+autocoverLen+"\" onkeyup=\"this.value=this.value.replace(/[^0-9]/g,'')\" onafterpaste=\"this.value=this.value.replace(/[^0-9]/g,'')\">"+
								"</label>"+
								"</section>"+
								"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
								"<label class=\"\">位置</label>"+
								"<label class=\"input\">" +
								"<select class=\"input-sm\">"+locstr+"</select>"+
								"</label>"+
								"</section>"+
								"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
								"<label class=\"\">符号</label>"+
								"<label class=\"input\"><input  type=\"text\"  class=\"input-sm\" value=\""+autocoverMark+"\"></label>"+
								"</section>";
					}
					String usemd5 = "";
					if (jo.containsKey("usemd5") && jo.getString("usemd5").equalsIgnoreCase("true")) {
						usemd5 = "checked=\"checked\"";
					}
					hbasekeys += "<tr><td style=\"text-align: left; vertical-align: middle;\">"+
							"<div id=\""+tbid+"_"+name+"\" style=\"clear:both;\">"+
							"<section class=\"pull-left no-padding col pull-width120\" style=\"margin: 2px\">"+
							"<label class=\"\">name</label>"+
							"<label class=\"input\"><input id=\"name_"+name+"\" type=\"text\" name=\"name_"+name+"\" placeholder=\"\" class=\"input-sm\" value=\""+name+"\"></label>"+
							"</section>"+
							"<section class=\"pull-left no-padding col pull-width80\" style=\"margin: 2px\">"+
							"<label class=\"\">类型</label>"+
							"<label class=\"input\"><select id=\"type_"+name+"\" class=\"input-sm\" onchange=\"getextra(this);\">"+ typeoptions +
							"</select></label>"+
							"</section>"+
							"<div style=\"float:left\">"+extra+"</div>"+
							"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
							"<label class=\"\">倒序</label>"+
							"<label class=\"checkbox\"><input  type=\"checkbox\"  id=\"reverse_"+name+"\" "+reverse+"><i></i></label>"+
							"</section>"+
							"<section class=\"pull-left no-padding col pull-width40\" style=\"margin: 2px\">"+
							"<label class=\"\">MD5(32)</label>"+
							"<label class=\"checkbox\"><input  type=\"checkbox\"  id=\"usemd5_"+name+"\" "+usemd5+"><i></i></label>"+
							"</section>"+
							"<section class=\"pull-left no-padding col pull-width60\" style=\"margin: 2px\">"+
							"<label class=\"\">自动补位</label>"+
							"<label class=\"checkbox\"><input  type=\"checkbox\"  onclick=\"autocover(this);\" id=\"autocover_"+name+"\" "+autocover+"><i></i><input type=\"input\"/></label>"+
							"</section>"+
							"<div style=\"float:left\">"+autocoverstr+"</div>"+
							"<section class=\"pull-left no-padding col pull-width120\" style=\"margin: 2px\">"+
							"<label class=\"\">使用固定值</label>"+
							"<label class=\"input\"><input id=\"value_"+name+"\" type=\"text\" name=\"value_"+name+"\" placeholder=\"\" class=\"input-sm\" value=\""+value+"\"></label>"+
							"</section>"+
							"<a href=\"####\" onclick=\"deleterowkey(this);\"><img border=\"0\" src=\"img/delete.gif\" width=\"20\" height=\"20\"></a>"+
							"</div>" +
							"</td></tr>";
				}
			}			
		}
		return hbasekeys;
	}

	@Override
	public String getHbasePro(int hbaseid) {
		// TODO Auto-generated method stub
		String hbasepro = "";
		TbHbaseExample example = new TbHbaseExample();
		Criteria critera = example.createCriteria();
		critera.andIdEqualTo(hbaseid);
		List<TbHbase> l = tbHbaseMapper.selectByExample(example);
		for (int i = 0;i < l.size(); i++) {
			String quorum = l.get(i).getQuorum();
			String rootdir = l.get(i).getRootdir();
			String usehostnameonly = l.get(i).getUsehostnameonly();
			String parent = l.get(i).getZnodeparent();
			hbasepro += "<tr><td style=\"text-align: left; vertical-align: middle;\">"+
					"<div>hbase.rootdir	:	<input id=\"rootdir\" type=\"text\" name=\"rootdir\" placeholder=\"\" class=\"input-sm\" value=\""+rootdir+"\"></div>"
					+ "<div>zookeeper.znode.parent	:	<input id=\"znodeparent\" type=\"text\" name=\"znodeparent\" placeholder=\"\" class=\"input-sm\" value=\""+parent+"\"></div>"
					+ "<div>hbase.zookeeper.quorum	:	<input id=\"quorum\" type=\"text\" name=\"quorum\" placeholder=\"\" class=\"input-sm\" value=\""+quorum+"\"></div>"
					+ "<div>hbase.client.usehostnameonly	:	<input id=\"usehostnameonly\" type=\"text\" name=\"usehostnameonly\" placeholder=\"\" class=\"input-sm\" value=\""+usehostnameonly+"\"></div>"
					+ "<div><input id=\"updatehbasepro\" class=\"btn btn-primary btn-sm\" type=\"button\" onclick=\"updatehbasepro();\" value=\"提交\"></div>"+
					"</td></tr>";
		}
		return hbasepro;
	}
	@Override
	public int updateRowkey(int tid,String rowkeyrule) {
		// TODO Auto-generated method stub
		TbRowkeyruleExample example = new TbRowkeyruleExample();
		com.smarttest.portal.pojo.TbRowkeyruleExample.Criteria critera = example.createCriteria();
		critera.andTidEqualTo(tid);
		List<TbRowkeyrule> l = tbRowkeyruleMapper.selectByExampleWithBLOBs(example);
		int code = 0;
		if (l.get(0).getRowkeyrule().isEmpty()) {
			TbRowkeyrule rule = l.get(0);
			rule.setRowkeyrule(rowkeyrule);
			code = tbRowkeyruleMapper.updateByExampleWithBLOBs(rule, example);
		}
		else{
			JSONArray ja1 = JSON.parseArray(l.get(0).getRowkeyrule());
			JSONArray ja2 = JSON.parseArray(rowkeyrule);
			TbRowkeyrule rule = l.get(0);
			if (ja1.containsAll(ja2) && ja2.containsAll(ja1)){
				
			}
			else {
				rule.setRowkeyrule(rowkeyrule);
				code = tbRowkeyruleMapper.updateByExampleWithBLOBs(rule, example);
			}
		}
		
		return code;
	}
	@Override
	public int updateHbasePro(int hid,String rootdir,String znodeparent,String quorum,String usehostnameonly) {
		// TODO Auto-generated method stub
		int code = 0;
		TbHbaseExample example = new TbHbaseExample();
		Criteria critera = example.createCriteria();
		critera.andIdEqualTo(hid);
		List<TbHbase> l = tbHbaseMapper.selectByExample(example);
		TbHbase hbase = l.get(0);
		if (hbase.getQuorum().equalsIgnoreCase(quorum) && hbase.getRootdir().equalsIgnoreCase(rootdir)
				&& hbase.getUsehostnameonly().equalsIgnoreCase(usehostnameonly)
				&& hbase.getZnodeparent().equalsIgnoreCase(znodeparent)) {
			
		}
		else {
			if (!hbase.getQuorum().equalsIgnoreCase(quorum)) {
				hbase.setQuorum(quorum);
			}
			if (!hbase.getRootdir().equalsIgnoreCase(rootdir)) {
				hbase.setRootdir(rootdir);
			}
			if (!hbase.getUsehostnameonly().equalsIgnoreCase(usehostnameonly)) {
				hbase.setUsehostnameonly(usehostnameonly);
			}
			if (!hbase.getZnodeparent().equalsIgnoreCase(znodeparent)) {
				hbase.setZnodeparent(znodeparent);
			}
			code = tbHbaseMapper.updateByExampleSelective(hbase, example);
		}	
		return code;
	}

	@Override
	public String getRowkeySearchStr(int tid) {
		// TODO Auto-generated method stub
		TbRowkeyruleExample example = new TbRowkeyruleExample();
		com.smarttest.portal.pojo.TbRowkeyruleExample.Criteria critera = example.createCriteria();
		critera.andTidEqualTo(tid);
		List<TbRowkeyrule> l = tbRowkeyruleMapper.selectByExampleWithBLOBs(example);
		String hbasekeystr = "";
		String rowkeyrule = l.get(0).getRowkeyrule();
		if (!rowkeyrule.isEmpty()) {
			JSONArray ja = JSON.parseArray(rowkeyrule);
			for (int j = 0; j < ja.size(); j++) {
				JSONObject jo = ja.getJSONObject(j);
				if (!jo.containsKey("value") || jo.getString("value").trim().isEmpty()) {
					if (jo.getString("type").equalsIgnoreCase("time")) {
						hbasekeystr += "<section class=\"pull-left no-padding col pull-width120\"><label id=\"tableid_"+tid+"\">请选择时间查询方式</label><label class=\"input\"><select id=\"timetype\" class=\"input-sm\" style=\"width:100%\" onchange=\"selectTimetype();\"><option value =\"timeinternal\" selected=\"selected\">时间段</option><option value =\"exacttime\">精确时间</option></select></label></section>";
						break;
					}
					else if (jo.getString("type").equalsIgnoreCase("timestamp")){
						hbasekeystr += "<section class=\"pull-left no-padding col pull-width120\"><label id=\"tableid_"+tid+"\">请选择时间查询方式</label><label class=\"input\"><select id=\"timetype\" class=\"input-sm\" style=\"width:100%\" onchange=\"selectTimetype();\"><option value =\"timeinternal\" selected=\"selected\">时间段</option><option value =\"exacttime\">精确时间戳</option></select></label></section>";
						break;
					} 
				}
			}
			for (int j = 0; j < ja.size(); j++) {
				JSONObject jo = ja.getJSONObject(j);
				if (!jo.containsKey("value") || jo.getString("value").trim().isEmpty()) {
					String name = jo.getString("name");
					String type = jo.getString("type");
					if (type.equalsIgnoreCase("time")) {
						String format = jo.getString("format");
						hbasekeystr += "<div id=\"hbase_selecttime\">"+
								"<div id=\"hbase_type1\">"+
								"<section class=\"pull-left no-padding col pull-width120\">"+
								"<label class=\"\">开始时间</label><label class=\"input\"><i class=\"icon-prepend fa fa-calendar\"></i><input name="+format+" type=\"text\" class=\"input-sm\" id=\"Hbase_starttime\" maxlength=\"40\" size=20 readonly=\"readonly\"></label>"+
								"</section>"+
								"<section class=\"pull-left no-padding col pull-width120\">"+
								"<label class=\"\">结束时间</label><label class=\"input\"><i class=\"icon-prepend fa fa-calendar\"></i><input name="+format+" type=\"text\" class=\"input-sm\" id=\"Hbase_endtime\" maxlength=\"40\" size=20 readonly=\"readonly\"></label>"+
								"</section>"+
								"</div>"+
								"<div id=\"hbase_type2\" style=\"display: none\">"+
								"<section class=\"pull-left no-padding col pull-width120\">"+
								"<label class=\"\">"+name+"</label><label class=\"input\"><i class=\"icon-prepend fa fa-calendar\"></i><input name="+format+" type=\"text\" class=\"input-sm\" id=\"Hbase_exacttime\" maxlength=\"40\" size=20 readonly=\"readonly\"></label>"+
								"</section>"+
								"</div>";
								
					}
					else if (type.equalsIgnoreCase("timestamp")) {
						String stamplen = jo.getString("stamplen");
						hbasekeystr += "<div id=\"hbase_selecttime\">"+
								"<div id=\"hbase_type1\">"+
								"<section class=\"pull-left no-padding col pull-width120\">"+
								"<label class=\"\">开始时间</label><label class=\"input\"><i class=\"icon-prepend fa fa-calendar\"></i><input name=\"yyyy-MM-dd HH:mm:ss\" type=\"text\" class=\"input-sm\" id=\"Hbase_starttime\" maxlength=\"40\" size=20 readonly=\"readonly\"></label>"+
								"</section>"+
								"<section class=\"pull-left no-padding col pull-width120\">"+
								"<label class=\"\">结束时间</label><label class=\"input\"><i class=\"icon-prepend fa fa-calendar\"></i><input name=\"yyyy-MM-dd HH:mm:ss\" type=\"text\" class=\"input-sm\" id=\"Hbase_endtime\" maxlength=\"40\" size=20 readonly=\"readonly\"></label>"+
								"</section>"+
								"</div>"+
								"<div id=\"hbase_type2\" style=\"display: none\">"+
								"<section class=\"pull-left no-padding col pull-width140\">"+
								"<label id=\""+name+"\">"+name+"("+stamplen+"位时间戳)"+"</label>"+
								"<label class=\"input\"><input id=\"rowkey_timestamp\" type=\"text\" name=\"rowkey_timestamp\" placeholder=\"\" class=\"input-sm\" ></label>"+
								"</section>"+
								"</div>"+
								"</div>";
					}
					else {
						hbasekeystr += "<div id=\"hb_"+name+"\">"+
								"<section class=\"pull-left no-padding col pull-width120\"><label id=\""+name+"\">"+name+"</label>"+
								"<label class=\"input\"><input id=\"rowkey_"+name+"\" type=\"text\" name=\"rowkey_"+name+"\" placeholder=\"\" class=\"input-sm\" ></label>"+
								"</section>"+
								"</div>";
					}
				}
				
			}
		}
		
		return hbasekeystr;
	}
	
	@Override
	public String getHbaseResult(int hid, int tid, JSONObject jo) {
		// TODO Auto-generated method stub
		String thead = "<thead><tr><th>rowkey</th><th>timestamp</th><th>family</th><th>qualifier</th><th>value</th>";	
		String tbody = "<tbody id=\"tbody\">";
		TbRowkeyruleExample example = new TbRowkeyruleExample();
		com.smarttest.portal.pojo.TbRowkeyruleExample.Criteria critera = example.createCriteria();
		critera.andTidEqualTo(tid);
		List<TbRowkeyrule> l = tbRowkeyruleMapper.selectByExampleWithBLOBs(example);
		String rowkeyrule = l.get(0).getRowkeyrule();
		String rowkey = "";
		String rowkeystart = "";
		String rowkeyend = "";
		Boolean istimeinternal = false;
		if(!rowkeyrule.isEmpty()) {
			JSONArray ja = JSON.parseArray(rowkeyrule);
			for (Object o : ja) {
				String name = ((JSONObject)o).getString("name");
				String type = ((JSONObject)o).getString("type");
				String reverse = ((JSONObject)o).getString("reverse");
				String usemd5 = ((JSONObject)o).getString("usemd5");
				String autocover = ((JSONObject)o).getString("autocover");
				if ( ((JSONObject)o).containsKey("value") && !((JSONObject)o).getString("value").trim().isEmpty()){
					String value = ((JSONObject)o).getString("value").trim();
					rowkey += value;
					rowkeystart += value;
					rowkeyend += value;
				}
				else {
					if (jo.containsKey("time") && type.contains("time")){
						Object time = jo.get("time");
						if (time instanceof JSONObject) {
							istimeinternal = true;
							String start = ((JSONObject) time).getString("start");
							String end = ((JSONObject) time).getString("end");
							String len = "";
							SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							if (type.equalsIgnoreCase("timestamp")) {
								len = ((JSONObject)o).getString("stamplen");
							}
							try {
								//10位时间戳
								if (len.equals("10")) {
									
									start = String.valueOf(sf.parse(start).getTime()/1000);							
									end = String.valueOf(sf.parse(end).getTime()/1000);
								}
								//13位时间戳
								else if(len.equals("13")){
									start = String.valueOf(sf.parse(start).getTime());
									end = String.valueOf(sf.parse(end).getTime());
								}
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							if ("true".equals(reverse)) {
								start = HbaseUtils.getReverseString(start);
								end = HbaseUtils.getReverseString(end);
							}
							rowkeystart += start;
							rowkeyend += end;
						}
						else {
							String timestr = jo.getString("time");
							if ("true".equals(reverse)) {
								timestr = HbaseUtils.getReverseString(timestr);
							}
							rowkey+=timestr;
						}
					}
					else if (jo.containsKey(name)) {
						String value = jo.getString(name);
						if (autocover.equalsIgnoreCase("true")) {
							//自动补位的长度
							int len = Integer.parseInt(((JSONObject)o).getString("autocoverLen"));
							int loc = 0;
							//在哪补位
							String locstr = ((JSONObject)o).getString("autocoverLoc");
							if (locstr.equalsIgnoreCase("before")) {
								loc = 0;
							}
							else if (locstr.equalsIgnoreCase("after")){
								loc = 1;
							}
							//补什么
							String mark = ((JSONObject)o).getString("autocoverMark");
							value = HbaseUtils.autoCover(value, len, loc, mark);
						}
						if ("true".equalsIgnoreCase(reverse)) {
							value = HbaseUtils.getReverseString(value);
						}
						//是否作MD5转码
						if (usemd5.equalsIgnoreCase("true")) {
							value = HbaseUtils.MD5(value);
						}
						rowkey+=value;
						rowkeystart += value;
						rowkeyend += value;
					}
				}			
			}
		}
		//获得hbase配置
		TbHbaseExample example1 = new TbHbaseExample();
		Criteria critera1 = example1.createCriteria();
		critera.andIdEqualTo(hid);
		List<TbHbase> l2 = tbHbaseMapper.selectByExample(example1);
		String quorum = l2.get(0).getQuorum();
		String rootdir = l2.get(0).getRootdir();
		String usehostnameonly = l2.get(0).getUsehostnameonly();
		String parent = l2.get(0).getZnodeparent();
		Map<String,String> m = new HashMap<>();
		m.put("hbase.zookeeper.quorum", quorum);
		m.put("hbase.rootdir", rootdir);
		m.put("hbase.client.usehostnameonly", usehostnameonly);
		m.put("zookeeper.znode.parent", parent);
		HbaseUtils hu = new HbaseUtils(m);
		//获得表名
		TbHtableExample example2 = new TbHtableExample();
		com.smarttest.portal.pojo.TbHtableExample.Criteria critera2 = example2.createCriteria();
		critera2.andIdEqualTo(tid);
		List<TbHtable> l3 = tbHtableMapper.selectByExample(example2);
		String tableName = l3.get(0).getTbname();
		if (istimeinternal) {
			List<Map<String, String>> resultlist = null;
			try {
				resultlist = hu.getResultScann(tableName, rowkeystart, rowkeyend);
				for (Map<String, String> rm : resultlist) {
	                	tbody += "<tr><th>"+rm.get("rowkey")+"</th><th>"+rm.get("timestamp")+"</th><th>"+rm.get("family")+"</th><th>"
	                			+rm.get("qualifier")+"</th><th>"
	                			+rm.get("value")+"</th></tr>";
	            }
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		else {
			try {
				Result r = hu.getResult(tableName, rowkey);
				if (!r.isEmpty()) {
					for (Cell c : r.listCells()) {
						tbody += "<tr><th>"+Bytes.toString(c.getRow())+"</th><th>"+String.valueOf(c.getTimestamp())+"</th><th>"
								+Bytes.toString(c.getFamily())+"</th><th>"
	                			+Bytes.toString(c.getQualifier())+"</th><th>"
	                			+Bytes.toString(c.getValue())+"</th></tr>";
					}
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		thead += "</tr></thead>";
		tbody += "</tbody>";
		return thead+tbody;
	}
	@Override
	public List<Map<String, String>> getHbaseResult_New(int hid, int tid, JSONObject jo) {
		// TODO Auto-generated method stub
//		String thead = "<thead><tr><th>rowkey</th><th>timestamp</th><th>family</th><th>qualifier</th><th>value</th>";	
//		String tbody = "<tbody id=\"tbody\">";
		TbRowkeyruleExample example = new TbRowkeyruleExample();
		com.smarttest.portal.pojo.TbRowkeyruleExample.Criteria critera = example.createCriteria();
		critera.andTidEqualTo(tid);
		List<TbRowkeyrule> l = tbRowkeyruleMapper.selectByExampleWithBLOBs(example);
		String rowkeyrule = l.get(0).getRowkeyrule();
		String rowkey = "";
		String rowkeystart = "";
		String rowkeyend = "";
		Boolean istimeinternal = false;
		if(!rowkeyrule.isEmpty()) {
			JSONArray ja = JSON.parseArray(rowkeyrule);
			for (Object o : ja) {
				String name = ((JSONObject)o).getString("name");
				String type = ((JSONObject)o).getString("type");
				String reverse = ((JSONObject)o).getString("reverse");
				String usemd5 = ((JSONObject)o).getString("usemd5");
				String autocover = ((JSONObject)o).getString("autocover");
				if ( ((JSONObject)o).containsKey("value") && !((JSONObject)o).getString("value").trim().isEmpty()){
					String value = ((JSONObject)o).getString("value").trim();
					rowkey += value;
					rowkeystart += value;
					rowkeyend += value;
				}
				else {
					if (jo.containsKey("time") && type.contains("time")){
						Object time = jo.get("time");
						if (time instanceof JSONObject) {
							istimeinternal = true;
							String start = ((JSONObject) time).getString("start");
							String end = ((JSONObject) time).getString("end");
							String len = "";
							SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
							if (type.equalsIgnoreCase("timestamp")) {
								len = ((JSONObject)o).getString("stamplen");
							}
							try {
								if (len.equals("10")) {
									
									start = String.valueOf(sf.parse(start).getTime()/1000);							
									end = String.valueOf(sf.parse(end).getTime()/1000);
								}
								else if(len.equals("13")){
									start = String.valueOf(sf.parse(start).getTime());
									end = String.valueOf(sf.parse(end).getTime());
								}
							} catch (ParseException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							if ("true".equals(reverse)) {
								start = HbaseUtils.getReverseString(start);
								end = HbaseUtils.getReverseString(end);
							}
							rowkeystart += start;
							rowkeyend += end;
						}
						else {
							String timestr = jo.getString("time");
							if ("true".equals(reverse)) {
								timestr = HbaseUtils.getReverseString(timestr);
							}
							rowkey+=timestr;
						}
					}
					else if (jo.containsKey(name)) {
						String value = jo.getString(name);
						if (autocover.equalsIgnoreCase("true")) {
							int len = Integer.parseInt(((JSONObject)o).getString("autocoverLen"));
							int loc = 0;
							String locstr = ((JSONObject)o).getString("autocoverLoc");
							if (locstr.equalsIgnoreCase("before")) {
								loc = 0;
							}
							else if (locstr.equalsIgnoreCase("after")){
								loc = 1;
							}
							String mark = ((JSONObject)o).getString("autocoverMark");
							value = HbaseUtils.autoCover(value, len, loc, mark);
						}
						if ("true".equalsIgnoreCase(reverse)) {
							value = HbaseUtils.getReverseString(value);
						}
						if (usemd5.equalsIgnoreCase("true")) {
							value = HbaseUtils.MD5(value);
						}
						rowkey+=value;
						rowkeystart += value;
						rowkeyend += value;
					}
				}			
			}
		}
		//获得hbase配置
		TbHbaseExample example1 = new TbHbaseExample();
		Criteria critera1 = example1.createCriteria();
		critera.andIdEqualTo(hid);
		List<TbHbase> l2 = tbHbaseMapper.selectByExample(example1);
		String quorum = l2.get(0).getQuorum();
		String rootdir = l2.get(0).getRootdir();
		String usehostnameonly = l2.get(0).getUsehostnameonly();
		String parent = l2.get(0).getZnodeparent();
		Map<String,String> m = new HashMap<>();
		m.put("hbase.zookeeper.quorum", quorum);
		m.put("hbase.rootdir", rootdir);
		m.put("hbase.client.usehostnameonly", usehostnameonly);
		m.put("zookeeper.znode.parent", parent);
		HbaseUtils hu = new HbaseUtils(m);
		//获得表名
		TbHtableExample example2 = new TbHtableExample();
		com.smarttest.portal.pojo.TbHtableExample.Criteria critera2 = example2.createCriteria();
		critera2.andIdEqualTo(tid);
		List<TbHtable> l3 = tbHtableMapper.selectByExample(example2);
		String tableName = l3.get(0).getTbname();
		List<Map<String, String>> resultlist = null;
		if (istimeinternal) {
			try {
				resultlist = hu.getResultScann(tableName, rowkeystart, rowkeyend);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}		
		}
		else {
			try {
				Result r = hu.getResult(tableName, rowkey);
				if (!r.isEmpty()) {
					for (Cell c : r.listCells()) {
						Map<String, String> ms = new HashMap<String, String>();
						ms.put("rowkey", Bytes.toString(c.getRow()));
						ms.put("timestamp", String.valueOf(c.getTimestamp()));
						ms.put("family", Bytes.toString(c.getFamily()));
						ms.put("qualifier",Bytes.toString(c.getQualifier()));
						ms.put("value", Bytes.toString(c.getValue()));
						resultlist.add(ms);
					}
				}
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return resultlist;
	}
	@Override
	public int addHbase(String name, String rootdir, String znodeparent,
			String quorum, String usehostnameonly) {
		// TODO Auto-generated method stub
		TbHbase hbase = new TbHbase();
		hbase.setId(null);
		hbase.setName(name);
		hbase.setQuorum(quorum);
		hbase.setRootdir(rootdir);
		hbase.setUsehostnameonly(usehostnameonly);
		hbase.setZnodeparent(znodeparent);
		return tbHbaseMapper.insert(hbase);
	}
	@Override
	public int addHtable(int hid, String name, int usemd5) {
		// TODO Auto-generated method stub
		TbHtable htable = new TbHtable();
		htable.setId(null);
		htable.setHid(hid);
		htable.setTbname(name);
		if (usemd5 == 1) {
			htable.setUsemd5(true);
		}
		else {
			htable.setUsemd5(false);
		}
		return tbHtableMapper.insert(htable);
	}
	@Override
	public int addRowkeyRule(int tid,String rowkeyrule) {
		// TODO Auto-generated method stub
		TbRowkeyrule hrowkeyrule = new TbRowkeyrule();
		hrowkeyrule.setId(null);
		hrowkeyrule.setTid(tid);
		hrowkeyrule.setRowkeyrule(rowkeyrule);
		return tbRowkeyruleMapper.insert(hrowkeyrule);
	}
}	
